package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsRole;
import com.qf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private DtsRoleMapper roleMapper;

    @Override
    public Set<String> queryRolesByIds(Integer[] ids) {
        Set<String> roleNameSet = new HashSet<>();

        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",ids);
        List<DtsRole> list = roleMapper.selectList(queryWrapper);

        if (list != null){
            for (DtsRole dtsRole : list) {
                roleNameSet.add(dtsRole.getName())  ;
            }
        }

        return roleNameSet;
    }

    @Override
    public List<DtsRole> findAllRoleList() {
        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        List<DtsRole> roleList = roleMapper.selectList(queryWrapper);
        return roleList;
    }

    @Override
    public IPage<DtsRole> findByPage(Integer page, Integer limit, String sort, String order, String rolename) {
        Page<DtsRole> pageParam = new Page<>(page, limit);
        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(rolename)){
            queryWrapper.like("name", rolename);
        }
        if ("asc".equalsIgnoreCase(order)){
            queryWrapper.orderByAsc(sort);
        }else{
            queryWrapper.orderByDesc(sort);
        }
        Page<DtsRole> pageResult = roleMapper.selectPage(pageParam, queryWrapper);
        return pageResult;
    }

    @Override
    public DtsRole add(DtsRole role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        role.setDeleted(false);
        roleMapper.insert(role);
        return role;
    }

    @Override
    public int update(DtsRole role) {
        role.setUpdateTime(new Date());
        return roleMapper.updateById(role);
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }
}
